package my.springboot3.utils;


import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class JwtUtil {
    //token密钥
    @Value("${jwt.key}")
    private static final String KEY = "三重门";




    /**
     *  接收业务数据,生成token并返回
     */
    public static String genToken(Map<String, Object> claims) {
        //生成token
        return JWT.create()
                .withClaim("claims", claims)//添加业务数据
                // .withExpiresAt(new Date(System.currentTimeMillis() + 1000 * 60 * 60 ))//设置过期时间
                .sign(Algorithm.HMAC256(KEY));//设置密钥
    }


    /**
     * 接收token,验证token,并返回业务数据
     */
    public static Map<String, Object> parseToken(String token) {
        return JWT.require(Algorithm.HMAC256(KEY))
                .build()
                .verify(token)
                .getClaim("claims")
                .asMap();
    }

}
